package com.valuegroup.smarttodo.data.service;

import com.valuegroup.smarttodo.data.model.AppStatus;
import com.valuegroup.smarttodo.data.model.GoalRelate;
import com.valuegroup.smarttodo.data.model.GoalRelateDao;
import com.valuegroup.smarttodo.data.model.Tag;
import com.valuegroup.smarttodo.data.model.TagDao;
import com.valuegroup.smarttodo.data.vo.TagVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TagService extends BaseService {

    private List<Long> findGoalRelatesToIdList(Long gid) {
        // 查询关联的TAG
        List<GoalRelate> relates = getGoalRelateDao().queryBuilder()
                .where(GoalRelateDao.Properties.RelateType.eq(AppStatus.GoalsRelateType.TAG))
                .where(GoalRelateDao.Properties.GoalId.eq(gid))
                .where(GoalRelateDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .list();

        List<Long> result = new ArrayList<>();
        for (GoalRelate r : relates) {
            result.add(r.getRelateId());
        }
        return result;
    }

    public List<TagVo> findTagForGoal(Long gid, boolean all) {
        List<TagVo> result = new ArrayList<>();

        // 查询关联
        List<Long> choseTag = findGoalRelatesToIdList(gid);

        if (all) {
            // 全部
            List<TagVo> temp = new ArrayList<>();
            for (Tag tag : getTagDao().loadAll()) {
                TagVo v = convertTagVo(tag);
                v.setChose(choseTag.contains(tag.getId()));

                if (v.isChose()) {
                    result.add(v);
                } else {
                    temp.add(v);
                }
            }
            // 控制已选的排前
            result.addAll(temp);
        } else {
            // 已选
            for (Tag tag : getTagDao().loadAll()) {
                if (choseTag.contains(tag.getId())) {
                    TagVo v = convertTagVo(tag);
                    v.setChose(choseTag.contains(tag.getId()));
                    result.add(v);
                }
            }
        }

        return result;
    }

    public List<TagVo> findAllTag() {
        List<TagVo> result = new ArrayList<>();
        // 全部
        for (Tag tag : getTagDao().loadAll()) {
            TagVo v = convertTagVo(tag);
            result.add(v);
        }
        return result;
    }


    public Tag getDefaultTag(String title) {
        Tag t = new Tag();
        t.setTitle(title);
        t.setSort(0);
        t.setCreatedDt(new Date());
        t.setUpdatedDt(t.getCreatedDt());
        t.setStatus(AppStatus.Status.NORMAL);
        return t;
    }


    public GoalRelate getDefaultGoalRelate(Long gid, Long rid, int relateType) {
        GoalRelate t = new GoalRelate();
        t.setGoalId(gid);
        t.setRelateId(rid);
        t.setRelateType(relateType);
        t.setCreatedDt(new Date());
        t.setUpdatedDt(t.getCreatedDt());
        t.setStatus(AppStatus.Status.NORMAL);
        return t;
    }

    public TagVo convertTagVo(Tag tag) {
        return new TagVo(tag.getId(), tag.getTitle(), false);
    }

    public void delTagForGoal(Long gid, Long tagId) {
        List<GoalRelate> relates = getGoalRelateDao().queryBuilder()
                .where(GoalRelateDao.Properties.RelateType.eq(AppStatus.GoalsRelateType.TAG))
                .where(GoalRelateDao.Properties.GoalId.eq(gid))
                .where(GoalRelateDao.Properties.RelateId.eq(tagId))
                .where(GoalRelateDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .list();
        getGoalRelateDao().deleteInTx(relates);
    }

    public void clearTagForGoal(Long gid) {
        List<GoalRelate> relates = getGoalRelateDao().queryBuilder()
                .where(GoalRelateDao.Properties.RelateType.eq(AppStatus.GoalsRelateType.TAG))
                .where(GoalRelateDao.Properties.GoalId.eq(gid))
                .where(GoalRelateDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .list();
        getGoalRelateDao().deleteInTx(relates);
    }

    public boolean existRelate(Long tagId) {
        // 查找标签是否与业务数据有关联
        int count = (int) getGoalRelateDao().queryBuilder()
                .where(GoalRelateDao.Properties.RelateType.eq(AppStatus.GoalsRelateType.TAG))
                .where(GoalRelateDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .count();
        return count > 0;
    }

    private void delExistRelate(Long tagId) {
        // 删除标签与业务数据的关联
        List<GoalRelate> relates = getGoalRelateDao().queryBuilder()
                .where(GoalRelateDao.Properties.RelateType.eq(AppStatus.GoalsRelateType.TAG))
                .where(GoalRelateDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .list();
        getGoalRelateDao().deleteInTx(relates);
    }

    public void delTag(Long tagId) {
        if (tagId == null || tagId == 0) {
            return;
        }

        Tag t = getTagDao().load(tagId);
        if (t == null) {
            return;
        }

        getTagDao().delete(t);
        // 删除标签与业务数据的关联
        delExistRelate(tagId);
    }

    public void addTagForGoal(Long gid, Long tagId) {
        getGoalRelateDao().save(getDefaultGoalRelate(gid, tagId, AppStatus.GoalsRelateType.TAG));
    }

    public void addTagAndSaveGoalRelate(String text, Long gid) {
        List<Tag> relates = getTagDao().queryBuilder()
                .where(TagDao.Properties.Title.eq(text))
                .where(TagDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .list();
        Tag tag;
        if (relates != null && relates.size() > 0) {
            tag = relates.get(0);
        } else {
            tag = getDefaultTag(text);
            getTagDao().save(tag);
        }

        // 保存关联关系
        addTagForGoal(gid, tag.getId());
    }
}
